package marksys.free.com.marksys.ui.user;

import android.net.Uri;
import android.os.Build;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import marksys.free.com.marksys.utils.CollectionUtils;

public class FileKeeper {

    private List<Uri> data = new ArrayList<>();

    public int size() {
        return data == null ? 0 : data.size();
    }

    public List<Uri> getAll() {
        return data;
    }

    public static final int MAX_COUNT = 6;

    public int allowSize() {
        return MAX_COUNT - data.size();
    }

    public int addAll(List<Uri> uris) {
        List<Uri> old = data;
        data = new ArrayList<>();
        data.addAll(old);
        data.addAll(uris);
        List<Uri> removeList = CollectionUtils.removeRepeat(data, true);
        return removeList.size();
    }

    /**
     *
     * @return 剩余data
     */
    public List<Uri> remove(Uri uri) {
        List<Uri> old = data;
        data = new ArrayList<>();
        data.addAll(old);
        if (data != null) {
            Iterator<Uri> iterator = data.iterator();
            while (iterator.hasNext()) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    if (Objects.equals(iterator.next(), uri)) {
                        iterator.remove();
                    }
                }
            }
        }
        return data;
    }
}
